{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "api_secret = '4T22waAUZVvfd1ejKn3E8t9m10tBEFMC'\n",
    "api_key ='9YObzUSK9CKURMgBMCzL9Kw0GblFzgIc'   # Replace with a valid Subscription Key here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. FaceSet Create\n",
    "import requests,json\n",
    "\n",
    "display_name = \"网新一班人脸集合\"\n",
    "outer_id = \"00001\"\n",
    "user_data = \"52人，20男生，32女生\"\n",
    "\n",
    "CreateFace_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'display_name':display_name,\n",
    "    'outer_id':outer_id,\n",
    "    'user_data':user_data\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(CreateFace_Url, params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'bed3cc66ef3d16c278097b4896b0a35e',\n",
       " 'time_used': 199,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1603366335,310f499e-9362-42fd-9b1a-4835b50c13f9',\n",
       " 'outer_id': '00001',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1、先导入为们需要的模块\n",
    "import requests\n",
    "\n",
    "\n",
    "api_secret = \"4T22waAUZVvfd1ejKn3E8t9m10tBEFMC\"\n",
    "# 2、输入我们API_Key\n",
    "api_key = '9YObzUSK9CKURMgBMCzL9Kw0GblFzgIc'  # Replace with a valid Subscription Key here.\n",
    "\n",
    "\n",
    "# 3、目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'http://newmedia.nfu.edu.cn/wcy/wp-content/uploads/2018/04/post_20180424__NFU_DoraHacks_imoji%E5%9B%A2%E9%98%9F.jpg'\n",
    "\n",
    "# 4、沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5、准备symbol ? 后面的数据\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "#  6、requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1603366423,2b6887e3-80c8-4477-a142-45b9f9f25a0f\",\"time_used\":338,\"faces\":[{\"face_token\":\"0fc7f1f2d3130a10f447e32ba8e60d59\",\"face_rectangle\":{\"top\":123,\"left\":138,\"width\":90,\"height\":90},\"attributes\":{\"gender\":{\"value\":\"Male\"},\"age\":{\"value\":23},\"smile\":{\"value\":99.995,\"threshold\":50.000},\"emotion\":{\"anger\":0.059,\"disgust\":0.059,\"fear\":0.059,\"happiness\":42.294,\"neutral\":52.431,\"sadness\":4.920,\"surprise\":0.179}}},{\"face_token\":\"f6d3aaea979f4878d02c4857b10d381e\",\"face_rectangle\":{\"top\":122,\"left\":373,\"width\":65,\"height\":65},\"attributes\":{\"gender\":{\"value\":\"Female\"},\"age\":{\"value\":37},\"smile\":{\"value\":98.866,\"threshold\":50.000},\"emotion\":{\"anger\":0.169,\"disgust\":7.280,\"fear\":0.138,\"happiness\":17.689,\"neutral\":8.067,\"sadness\":66.519,\"surprise\":0.138}}},{\"face_token\":\"cbf16a79fda287f240f72d95b37f33a6\",\"face_rectangle\":{\"top\":46,\"left\":675,\"width\":54,\"height\":54},\"attributes\":{\"gender\":{\"value\":\"Male\"},\"age\":{\"value\":23},\"smile\":{\"value\":100.000,\"threshold\":50.000},\"emotion\":{\"anger\":0.000,\"disgust\":0.000,\"fear\":0.000,\"happiness\":99.996,\"neutral\":0.001,\"sadness\":0.002,\"surprise\":0.001}}},{\"face_token\":\"133a149251f228a7ac89b3f248142865\",\"face_rectangle\":{\"top\":98,\"left\":235,\"width\":53,\"height\":53},\"attributes\":{\"gender\":{\"value\":\"Female\"},\"age\":{\"value\":22},\"smile\":{\"value\":99.997,\"threshold\":50.000},\"emotion\":{\"anger\":0.000,\"disgust\":0.001,\"fear\":0.000,\"happiness\":99.990,\"neutral\":0.009,\"sadness\":0.000,\"surprise\":0.000}}},{\"face_token\":\"36842306b53f88a77c857dce41db23af\",\"face_rectangle\":{\"top\":75,\"left\":444,\"width\":52,\"height\":52},\"attributes\":{\"gender\":{\"value\":\"Female\"},\"age\":{\"value\":28},\"smile\":{\"value\":99.999,\"threshold\":50.000},\"emotion\":{\"anger\":0.001,\"disgust\":0.001,\"fear\":0.027,\"happiness\":99.933,\"neutral\":0.027,\"sadness\":0.010,\"surprise\":0.001}}},{\"face_token\":\"1692168487bcbfe7389322c0c159d679\",\"face_rectangle\":{\"top\":98,\"left\":537,\"width\":48,\"height\":48}}],\"image_id\":\"TfC1AByqtXhfuQ6DruqVww==\",\"face_num\":6}\\n'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603366423,2b6887e3-80c8-4477-a142-45b9f9f25a0f',\n",
       " 'time_used': 338,\n",
       " 'faces': [{'face_token': '0fc7f1f2d3130a10f447e32ba8e60d59',\n",
       "   'face_rectangle': {'top': 123, 'left': 138, 'width': 90, 'height': 90},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 23},\n",
       "    'smile': {'value': 99.995, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.059,\n",
       "     'disgust': 0.059,\n",
       "     'fear': 0.059,\n",
       "     'happiness': 42.294,\n",
       "     'neutral': 52.431,\n",
       "     'sadness': 4.92,\n",
       "     'surprise': 0.179}}},\n",
       "  {'face_token': 'f6d3aaea979f4878d02c4857b10d381e',\n",
       "   'face_rectangle': {'top': 122, 'left': 373, 'width': 65, 'height': 65},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 37},\n",
       "    'smile': {'value': 98.866, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.169,\n",
       "     'disgust': 7.28,\n",
       "     'fear': 0.138,\n",
       "     'happiness': 17.689,\n",
       "     'neutral': 8.067,\n",
       "     'sadness': 66.519,\n",
       "     'surprise': 0.138}}},\n",
       "  {'face_token': 'cbf16a79fda287f240f72d95b37f33a6',\n",
       "   'face_rectangle': {'top': 46, 'left': 675, 'width': 54, 'height': 54},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 23},\n",
       "    'smile': {'value': 100.0, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.0,\n",
       "     'disgust': 0.0,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 99.996,\n",
       "     'neutral': 0.001,\n",
       "     'sadness': 0.002,\n",
       "     'surprise': 0.001}}},\n",
       "  {'face_token': '133a149251f228a7ac89b3f248142865',\n",
       "   'face_rectangle': {'top': 98, 'left': 235, 'width': 53, 'height': 53},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 22},\n",
       "    'smile': {'value': 99.997, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.0,\n",
       "     'disgust': 0.001,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 99.99,\n",
       "     'neutral': 0.009,\n",
       "     'sadness': 0.0,\n",
       "     'surprise': 0.0}}},\n",
       "  {'face_token': '36842306b53f88a77c857dce41db23af',\n",
       "   'face_rectangle': {'top': 75, 'left': 444, 'width': 52, 'height': 52},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 28},\n",
       "    'smile': {'value': 99.999, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.001,\n",
       "     'disgust': 0.001,\n",
       "     'fear': 0.027,\n",
       "     'happiness': 99.933,\n",
       "     'neutral': 0.027,\n",
       "     'sadness': 0.01,\n",
       "     'surprise': 0.001}}},\n",
       "  {'face_token': '1692168487bcbfe7389322c0c159d679',\n",
       "   'face_rectangle': {'top': 98, 'left': 537, 'width': 48, 'height': 48}}],\n",
       " 'image_id': 'TfC1AByqtXhfuQ6DruqVww==',\n",
       " 'face_num': 6}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# requests 巧妙的方法   r = response\n",
    "results = r.json() # \n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "GetDetail_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'outer_id':outer_id,\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(GetDetail_Url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'bed3cc66ef3d16c278097b4896b0a35e',\n",
       " 'tags': '',\n",
       " 'time_used': 91,\n",
       " 'user_data': '52人，20男生，32女生',\n",
       " 'display_name': '网新一班人脸集合',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603366476,0b3e6dd8-62c7-4e19-93e9-e76bfdedd172',\n",
       " 'outer_id': '00001'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "AddFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'bed3cc66ef3d16c278097b4896b0a35e',\n",
    "    'face_tokens':'1692168487bcbfe7389322c0c159d679',\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(AddFace_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'bed3cc66ef3d16c278097b4896b0a35e',\n",
       " 'time_used': 454,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1603367027,ee9d60c5-4602-468d-85f8-9921569c92ac',\n",
       " 'outer_id': '00001',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "RemoveFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/removeface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'bed3cc66ef3d16c278097b4896b0a35e',\n",
    "    'face_tokens':'1692168487bcbfe7389322c0c159d679',\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(RemoveFace_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'bed3cc66ef3d16c278097b4896b0a35e',\n",
       " 'face_removed': 1,\n",
       " 'time_used': 166,\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603367078,ab0684b9-ef61-4894-856e-e1dbdb13728d',\n",
       " 'outer_id': '00001',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "Update_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'bed3cc66ef3d16c278097b4896b0a35e',\n",
    "    'user_data':\"53人，21男生，32女生\",\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(Update_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'bed3cc66ef3d16c278097b4896b0a35e',\n",
       " 'request_id': '1603367138,0a57be0c-2a34-483a-980e-3c45325449bf',\n",
       " 'time_used': 72,\n",
       " 'outer_id': '00001'}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "liudehua01 = \"https://gss0.baidu.com/9fo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/7c1ed21b0ef41bd57f7f20ff57da81cb39db3d89.jpg\"\n",
    "liudehua02 = \"https://tse3-mm.cn.bing.net/th/id/OIP.Xz3HbYZeNrdUnGJ7vXNzsQHaKO?pid=Api&rs=1\"\n",
    "wangzulan = \"https://tse3-mm.cn.bing.net/th/id/OIP.ZnXeGoVYT4jQudiPOGZn3QAAAA?pid=Api&rs=1\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "Compare_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "payload ={\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'image_url1':liudehua01,\n",
    "    'image_url2':wangzulan\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(Compare_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faces1': [{'face_rectangle': {'width': 824,\n",
       "    'top': 871,\n",
       "    'left': 1114,\n",
       "    'height': 824},\n",
       "   'face_token': '6f45a8481db4f3a11b12588fc1a7992c'}],\n",
       " 'faces2': [{'face_rectangle': {'width': 86,\n",
       "    'top': 91,\n",
       "    'left': 65,\n",
       "    'height': 86},\n",
       "   'face_token': '1ee45cc430e0f7575f26feaf2468d4e8'}],\n",
       " 'time_used': 2484,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'confidence': 26.085,\n",
       " 'image_id2': 'g6kg8zfyOouG6ftP+GvEfg==',\n",
       " 'image_id1': 'KIOXEC2V/MyL4zuopAcNig==',\n",
       " 'request_id': '1603367185,aa767eb6-4d7c-4cc4-aec6-cab8909bef5b'}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "Detect_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = liudehua01\n",
    "\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(Detect_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603367220,5e9e862f-e43f-42b6-a49e-804caacb3df0',\n",
       " 'time_used': 1126,\n",
       " 'faces': [{'face_token': '8c485815205a9291ec1bec586903c41c',\n",
       "   'face_rectangle': {'top': 871, 'left': 1114, 'width': 824, 'height': 824},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 59},\n",
       "    'smile': {'value': 99.998, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.0,\n",
       "     'disgust': 0.047,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 99.945,\n",
       "     'neutral': 0.0,\n",
       "     'sadness': 0.007,\n",
       "     'surprise': 0.0}}}],\n",
       " 'image_id': 'KIOXEC2V/MyL4zuopAcNig==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
